<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/10/8
 * Time: 17:36
 */

namespace app\admin\controller;

use app\admin\service\FileService;
use lib\common\CommonController;
use lib\annotations\Permission;
use lib\exception\ParamsException;

class File extends CommonController
{
    /**
     * 单文件上传
     * @Permission
     * @param FileService $service
     * @return void
     */
    public function uploadSingle (FileService $service): void {
        $files = $this->request->file();
        if (empty($files)) {
            throw new ParamsException('请选择文件');
        }
        $ret = null;
        foreach ($files as $file) {
            $ret = $service->uploadSingle($file);
            break;
        }
        $this->success($ret);
    }


    /**
     * 多文件上传
     * @Permission
     * @param FileService $service
     * @return void
     */
    public function uploadMultiple (FileService $service): void {
        $files = $this->request->file();
        if (empty($files)) {
            throw new ParamsException('请选择文件');
        }
        $data = $service->uploadMultiple($files);
        $this->success($data);
    }
}
